package com.pet.server.ext.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringEscapeUtils;


public class StringUtil {
	private static final String BLANK = "";
	public static void main(String[] args){
		System.out.println(StringUtil.createId(32));
		
		
		//"003002.matches((000000|003001025))"
//		
//		String city_name="北京市";
//		
//		System.out.println(city_name.replaceAll("(省|市|区)", ""));
//		
//		System.out.println("003002025".matches("^.*.*$"));
//		
//		
//		System.out.println("15522328241".matches("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"));
		
		/**
		boolean b = "11".matches("[0-12]");
		System.out.println(b);
		
		
		System.out.println( MD5Util.MD5Encode("111111", null));
		
		
		String a = "";
		JSONObject o = new JSONObject();
		o.put("haha", null);
		o.accumulate("hahah", null);
		o.element("asd", "a");
		System.out.println(o.toString());
		*/
		//Double b= 20000d;
		//System.out.println(StringUtil.createUuid("aaa", "bb"));
		//System.out.println(StringEscapeUtils.escapeSql(null));
		//String reg= "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		//System.out.println("fujia_n  yd@163.@com".matches(reg));
		/***
		StringBuffer sbuf = new StringBuffer("com.pet.server.ext.");
		String uri = "/huaext/rest/2.0/product/list";
		uri = uri.replaceAll(".*\/rest/2.0/", "");
		String[] parts = uri.split("\\/");
		sbuf.append(parts[0]+".");
		sbuf.append(Character.toUpperCase(parts[1].charAt(0)));
		sbuf.append(parts[1].substring(1));
		System.out.println(sbuf.toString());
		System.out.println("/huaext/rest/2.0/product/list".matches(".*\/rest/2.0/.*"));

		*/
	}
	
	
	
	public static boolean isEmpty(String s){
		return s == null || BLANK.compareTo(s) == 0;
	}
	public static boolean isEmpty(Object str) {
		return str == null || BLANK.equals(str);
	}
	public static boolean isEmpty(Object ...strs){
		for(Object str:strs){
			if(isEmpty(str)){
				return true;
			}
		}
		return false;
	}
	public static String dealNull(String str) {
		return str != null ? str.trim() : BLANK;
	}
	public static String dealEmpty(String str, String defaultVal) {
		return !isEmpty(str) ? str.trim() : defaultVal;
	}
	
	public static String dealNull(String str, String defaultVal) {
		return str != null ? str.trim() : defaultVal;
	}
	public static String dealNull(Object str,String defaultValue){
		return str != null ? str.toString().trim() : defaultValue;
	}
	public static String dealNull(Object obj) {
		String str = BLANK;
		if(obj!=null){
			if(obj instanceof String){
				str = (String)obj;
			}else{
				str = obj.toString();
			}
		}
		return str;
	}
	
	
	public static String getExtensionName(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int i = filename.lastIndexOf('.');
			if ((i > -1) && (i < (filename.length() - 1))) {
				return filename.substring(i + 1);
			}
		}
		return null;
	}
	public static String getFileName(String filepath){
		if ((filepath != null) && (filepath.length() > 0)) {
			int i = filepath.lastIndexOf('/');
			if ((i > -1) && (i < (filepath.length() - 1))) {
				return filepath.substring(i + 1);
			}
		}
		return filepath;
	}
	public static String getFilePath(String filepath){
		if ((filepath != null) && (filepath.length() > 0)) {
			int i = filepath.lastIndexOf('/');
			if ((i > -1) && (i < (filepath.length() - 1))) {
				return filepath.substring(0,i + 1);
			}
		}
		return filepath;
	}
	public static boolean equals(Object value1, Object value2) {
		boolean is = false;
		if (value1 == value2) { // is null or self
			return is = true;
		}
		if (value1 != null && value2 != null) { // is not null;
			return value1.equals(value2);
		}
		return is;
	}
	public static int random(int n){
		if(n==0) return 0;
		Random random = new Random();
		return Math.abs(random.nextInt())%n;
	}
	public static String formatOrderId(Date date,Integer orderId){
        String prefix = DateUtil.getFormatDate(date, "yyyyMM");// createdTime.substring(0, tIndex).replace("-", "").substring(0, 6);        
        return String.format("%s%06d", prefix, orderId);
    }
	public static String createUuid(String mac,String imei){
		return MD5Util.MD5Encode(mac.replaceAll(":","")+imei, null);
	}
	public static String encode(String str, final String encoding) {
		try {
			if (!isEmpty(str)) {
				str = URLEncoder.encode(str, encoding);
			}
		} catch (Exception e) {
		}
		return str;
	}
	//生成问答的sessionid
	public static String createSessionid(Integer userId,Integer requestId){
		return "sessionid"+userId+requestId;
	}
	public static String filterCityName(String cityName){
		if(!isEmpty(cityName)){
			return cityName.replaceAll("(省|市|区|县)", "");
		}
		return "";
	}
	
	public static String createId(Integer length) { 
		String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";     
	    Random random = new Random();     
	    StringBuffer sb = new StringBuffer();     
	    for (int i = 0; i < length; i++) {     
	        int number = random.nextInt(base.length());     
	        sb.append(base.charAt(number));     
	    }     
	    return sb.toString();     
	 } 
	public static String createCode(Integer length) { 
		String base = "123456789";     
	    Random random = new Random();     
	    StringBuffer sb = new StringBuffer();     
	    for (int i = 0; i < length; i++) {     
	        int number = random.nextInt(base.length());     
	        sb.append(base.charAt(number));     
	    }     
	    return sb.toString();     
	 } 
	public static String appendCss(String server,String context){
		if(isEmpty(context)){
			return null;
		}
		String cssFile="<link rel=\"stylesheet\" href=\""+server+"/css/1024.css\"/>";
		return cssFile+context;
	}
}